<template>
  <comForm :form-info="props.formInfo" />
  <div style="margin-top: 16px">
    <a-space :size="8">
      <a-button
        v-if="!formInfo.contentUrl"
        v-permission="['pms:order:updateOrderBodyText']"
        @click="createContract"
        >生成正文</a-button
      >
      <a-button
        v-if="formInfo.contentUrl"
        v-permission="['pms:order:updateOrderBodyText']"
        @click="editDocHandle"
        >编辑正文</a-button
      >
      <a-button
        v-if="formInfo.contentUrl"
        v-permission="['pms:order:updateOrderBodyText']"
        @click="delDocHandle"
        >删除正文</a-button
      >
      <a-button v-if="formInfo.contentUrl" @click="readDocHandle"
        >查看正文</a-button
      >
    </a-space>
  </div>
</template>

<script lang="ts" setup>
  import { ref, defineProps, onMounted, defineEmits, watch } from 'vue';
  import { Message } from '@arco-design/web-vue';
  import { info } from '@/common/utils/modal-promise';
  import {
    getTemplateInfo,
    updateOrderBodyText,
  } from '@/api/purchase-order/purchaseOrderPreparation';
  import ntko from '@/common/service/ntko/index';
  import comForm from './com-form.vue';

  const props = defineProps({
    formInfo: {
      type: Object,
      default: null,
    },
  });
  const emit = defineEmits(['success']);
  const creatDocUrl = ref('');
  const readDocUrl = ref(props.formInfo.contentUrl);

  const docId = ref('');
  // 生成正文
  const createContract = () => {
    console.log('creatDocUrl.value', creatDocUrl.value);
    ntko.createContractDoc(creatDocUrl.value, {
      secretLevel: props.formInfo.secretLevel,
      mId: props.formInfo.id,
      bizType: '3',
      dataId: props.formInfo.id,
    });
  };
  // 编辑正文
  const editDocHandle = async () => {
    docId.value = await ntko.editDoc(readDocUrl.value, {
      secretLevel: props.formInfo.secretLevel,
      dataId: props.formInfo.id, // 数据id，用于save之后，确认是哪一条数据
    });
  };
  // 删除
  const delDocHandle = async () => {
    await info({
      title: '您确定要删除正文？',
      content: '',
      hideCancel: false,
    });
    const params = {
      id: props.formInfo.id,
      contentUrl: '',
    };
    const res1: any = await updateOrderBodyText(params);
    if (res1.success) {
      Message.success('删除成功！');
      emit('success');
    }
  };
  // 查看正文
  const readDocHandle = async () => {
    docId.value = await ntko.readDoc(readDocUrl.value);
  };
  // 监听文档保存成功了
  ntko.on('save', async (res: any) => {
    const params = {
      id: res.dataId,
      contentUrl: res.data,
    };
    const res1: any = await updateOrderBodyText(params);
    if (res1.success) {
      emit('success');
    }
  });

  // 监听文档保存成功了
  ntko.on('close', () => {
    console.log('1');
  });
  watch(
    () => props.formInfo,
    () => {
      readDocUrl.value = props.formInfo.contentUrl;
    },
    {
      deep: true,
    }
  );
  onMounted(async () => {
    const res: any = await getTemplateInfo(props.formInfo.mdsTempletId);
    if (res.code === 200) {
      creatDocUrl.value = res.data.templateContent;
    }
  });
</script>
